<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-form :inline="true" :model="queryParams" class="demo-form-inline" label-width="120px">
      <el-form-item label="费用类别名称" prop="projectNo">
        <el-input v-model="queryParams.accountName" placeholder="请输入费用类别名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">查询</el-button>
        <!-- <el-button type="primary" link @click="areConditionFold">
            {{ conditionFold ? '展开' : '收起' }}
          </el-button> -->
      </el-form-item>
    </el-form>

    <!-- 数据展示表格 -->
    <el-row :gutter="10" class="mb8">
      <!-- 操作行 -->
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Upload"
          v-if="false"
          @click="handleUpload"
          >导入</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          v-if="false"
          @click="handleExport"
          >导出</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" v-show="!multiple" @click="handleDelete">批量删除</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="tableData" style="width: 100%" max-height="500"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column prop="accountName" label="费用类别名称" align="center" />
      <el-table-column label="操作" width="180" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:class:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:class:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 新增修改弹框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="postRef" :model="formData" :rules="rules" label-width="120px">
        <el-form-item label="费用类别名称" prop="accountName">
          <el-input v-model="formData.accountName" placeholder="请输入费用类别名称" class="el-width">
          </el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ElMessage, ElMessageBox } from 'element-plus'
import { ref, reactive } from "vue";
import {
  listManagement,
  getManagement,
  addManagement,
  updateManagement,
  delManagement,
  listTwoDept,
  listPaydept,
} from "@/api/cost/accountClass";
import { pinyin } from "pinyin-pro";

// 字典引入
const { proxy } = getCurrentInstance();
const { payment_type, construction_flag } = proxy.useDict(
  "payment_type",
  "construction_flag"
);

//部门下拉树
const deptOptions = ref();
const constructionList = ref();
const PaydeptList = ref();
const queryPaydeptList = ref();
// 表格数据
const tableData = ref([]);
const loading = ref(true);
// 数据总数
const total = ref(0);
const open = ref(false);
const title = ref();
const update = ref(true);
//打开弹框

// 删除的ids
const ids = ref([]);
// 单选
const single = ref(true);
// 多选
const multiple = ref(true);
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
});
// 表单字段
const formData = ref({
  accountName: "",
  wellNo: "",
  shortNo: "",
  projectType: "",
  deptId: "",
  deptThreeId: "",
  startTime: "",
  completeMonth: "",
  constructionId: "",
  constructionAddress: "",
  constructionContent: "",
  paymentdeptId: "",
});
// 表单规则
const rules = ref({
  accountName: [{ required: true, message: "名称不能为空", trigger: "blur" }],
});
// 表单重置
const reset = () => {
  formData.value = {
    accountName: "",
  };
  proxy.resetForm("postRef");
};
//获取列表数据
const getList = () => {
  loading.value = true;
  listManagement(queryParams.value)
    .then((res) => {
      tableData.value = res.rows;
      total.value = res.total;
      loading.value = false;
    })
    .catch((error) => {
      console.log(error);
    });
  // 查询框付款单位
  listPaydept().then((res) => {
    queryPaydeptList.value = res.rows;
  });
};
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "新增";
  update.value = false;
}
/** 编辑按钮操作 */
function handleUpdate(row) {
  reset();
  formData.value.accountName = row.accountName;
  formData.value.id = row.id;
  open.value = true;
  title.value = "修改";
  update.value = true;
}
// 提交
const submitForm = () => {
  proxy.$refs["postRef"].validate((valid) => {
    if (valid) {
      if (update.value) {
        updateManagement(formData.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addManagement(formData.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
};
// 取消
const cancel = () => {
  open.value = false;
  reset();
};
/** 删除按钮操作 */
function handleDelete(row) {
  ElMessageBox.confirm("确认删除此项?", "警告！", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      const delId = row.id || ids.value;
      delManagement(delId).then((res) => {
        getList();
        ElMessage({
          type: "success",
          message: "删除成功",
        });
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消删除",
      });
    });
}
/**导出按钮操作 */
function handleExport() {
  proxy.download(
    "/system/management/export",
    {
      ...queryParams.value,
    },
    `项目列表.xlsx`
  );
}

//查询列表
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
getList();
</script>

<style lang="scss" scoped>
.el-row {
  margin-bottom: 20px;
}

.search-form {
  padding-left: 20px;
}

.el-width {
  width: 340px;
}
</style>
